<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Cardtype;
use app\admin\model\Luntan;
use think\Paginator;
class Index extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data=Cardtype::select();
        return $this->fetch('addluntan',['data'=>$data]);
    }
    public function do_luntan(Request $request){
        $files=$_FILES['thumbs'];
        $count=count($files['tmp_name']);
        $thumb='';
        for($i=0;$i<$count;$i++){
            $filename='../public/images/'.time().$i.'.png';
            $str= move_uploaded_file($files['tmp_name'][$i], $filename);
            if($str){
                $thumb.=substr($filename,16).',';
            }
        }
        $addlun=new Luntan();
        $addlun->thumb= substr($thumb,1,-1);
        $addlun->title=$request->param('title');
        $addlun->content=$request->param('content');
        $addlun->times=time();
        $addlun->type_id=$request->param('type');
        if($addlun->save()){
            $this->success('发布成功','/admin/index/luntanlist');
        }else{
            $this->error('发布失败');
        }
    }
    public function luntanlist(){

        return $this->fetch('luntan');

    }
    public function doluntanlist(Request $request){
        $list=Luntan::alias('l')
            ->join('cardtype c', 'c.id = l.type_id')
            ->join('user1 u','l.user_id = u.id')->where('dstaust',1)
            ->field('l.id,l.title,l.times,l.content,l.sum,l.give,l.ping,c.cardtype,l.thumb,u.username')
            ->paginate(5)->toArray();
//    $count=count($list);
////        var_dump($list['data'][0]['thumb']);
//       $arr=array();
//       for($i=0;$i<=$count;$i++){
//          $arr.=$list['data'][$i]['thumb'];
//       }
//        var_dump($arr);

            return json(['data'=>$list]);
    }
   public function luntandel(Request $request){
       $ids=$request->param('ids');
       $data=Luntan::get($ids);
       $data->dstaust=0;
       $data->save();
       return json(['code'=>200,'data'=>'删除成功']);
   }

    public function luntanupd(Request $request){
        $id=$request->param('id');
        // print_r($id);die;
        $data=Luntan::get($id);
        $type=Cardtype::select();
        return $this->fetch('luntanupd',['user'=>$data,'type'=>$type]);

    }

    public function do_luntanupd(Request $request){
        $id=$request->param('id');

        $files=$_FILES['thumbs'];
        $count=count($files['tmp_name']);
        $thumb='';
        for($i=0;$i<$count;$i++){
            $filename='../public/images/'.time().$i.'.png';
            $str= move_uploaded_file($files['tmp_name'][$i], $filename);
            if($str){
                $thumb.=substr($filename,16).',';
            }
        }
        $data=Luntan::get($id);
        $data->title=$request->param('title');
        $data->thumb= substr($thumb,1,-1);
        $data->content=$request->param('content');
        $data->times=time();
        $data->type_id=$request->param('type_id');
        if($data->save()){
            $this->success('修改成功','/admin/index/luntanlist');
        }else{
            $this->error('修改失败');
        }
    }



}
